package cn.fl.product.service.impl;

import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.fl.capital.constant.CapPrjArInfoConstant;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.product.service.ISplCapPrjArInfoService;
import cn.fl.product.service.ISplInfoService;
import fl.customer.facade.customer.api.ICustomerQueryApiService;
import fl.customer.facade.customer.condition.CstBscInfoQueryCondition;
import fl.customer.facade.customer.dto.CstBscInfoDTO;
import fl.mongo.facade.capital.api.ICapPriArInfoMongoService;
import fl.mongo.facade.capital.dto.CapPriArInfoDTO;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.lnkcomp.api.IPrjLnkCompApiService;
import fl.spl.facade.lnkcomp.api.IPrjLnkCompQueryApiService;
import fl.spl.facade.lnkcomp.condition.PrjLnkCompQueryCondition;
import fl.spl.facade.lnkcomp.dto.PrjLnkCompDTO;
import fl.spl.facade.spl.api.ISplInfoApiService;
import fl.spl.facade.spl.api.ISplInfoQueryApiService;
import fl.spl.facade.spl.condition.SplInfoQueryCondition;
import fl.spl.facade.spl.dto.SplInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SplCapPrjArInfoServiceImpl implements ISplCapPrjArInfoService {
    @Autowired
    private ICapPriArInfoMongoService capPriArInfoMongoService ;
    @Autowired
    private ISplInfoApiService splInfoApiService ;
    @Autowired
    private IPrjLnkCompQueryApiService prjLnkCompQueryApiService ;
    @Autowired
    private ICustomerQueryApiService customerQueryApiService;
    @Override
    public List<CapPriArInfoDTO> getCapPrjArInfoDTOByDataIds(List<Long> ids, String type, Date time) throws BaseException {
        List<CapPriArInfoDTO> list = this.capPriArInfoMongoService.getByListCondition(ids,type,time);
        if(CollectionUtils.isEmpty(list)){
            list = new ArrayList<>();
            CapPriArInfoDTO capPriArInfoDTO = null ;
            if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_4.equals(type)){
                SplInfoQueryCondition splInfoQueryQC = new SplInfoQueryCondition();
                splInfoQueryQC.setIds(ids);
                List<SplInfoDTO> splInfoDTOList = splInfoApiService.searchSplInfoList(splInfoQueryQC);
                if(CollectionUtils.isNotEmpty(splInfoDTOList)){
                    for (SplInfoDTO dto:splInfoDTOList) {
                        capPriArInfoDTO = new CapPriArInfoDTO();
                        capPriArInfoDTO.setMainId(dto.getId());
                        capPriArInfoDTO.setMainNm(dto.getSplNm());
                        capPriArInfoDTO.setId(dto.getId());
                        capPriArInfoDTO.setMainTypCd(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_4);
                        list.add(capPriArInfoDTO);
                    }
                }
            }else if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_3.equals(type)){
                PrjLnkCompQueryCondition lnkCompQueryQC = new PrjLnkCompQueryCondition();
                lnkCompQueryQC.setIdList(ids);
                List<PrjLnkCompDTO> prjLnkCompDTOList =  this.prjLnkCompQueryApiService.getPrjLnkCompByCondition(lnkCompQueryQC);
                if(CollectionUtils.isNotEmpty(prjLnkCompDTOList)){
                    for (PrjLnkCompDTO dto:prjLnkCompDTOList) {
                        capPriArInfoDTO = new CapPriArInfoDTO();
                        capPriArInfoDTO.setMainId(dto.getId());
                        capPriArInfoDTO.setMainNm(dto.getLnkCompNm());
                        capPriArInfoDTO.setId(dto.getId());
                        capPriArInfoDTO.setMainTypCd(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_3);
                        list.add(capPriArInfoDTO);
                    }
                }
            }else if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_1.equals(type)){
                CstBscInfoQueryCondition cstBscInfoQueryQC = new CstBscInfoQueryCondition();
                cstBscInfoQueryQC.setIds(ids);
                cstBscInfoQueryQC.setSysCd(CurrentThreadContext.getCurrentSysCd());
                cstBscInfoQueryQC.setOrgCd(SplSystemConstant.DEFAULT_ORG_CD);
                cstBscInfoQueryQC.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
                List<CstBscInfoDTO> cstBscInfoDTOList = this.customerQueryApiService.searchCstBscInfoList(cstBscInfoQueryQC);
                if(CollectionUtils.isNotEmpty(cstBscInfoDTOList)){
                    for (CstBscInfoDTO dto:cstBscInfoDTOList) {
                        capPriArInfoDTO = new CapPriArInfoDTO();
                        capPriArInfoDTO.setMainId(dto.getId());
                        capPriArInfoDTO.setMainNm(dto.getCstNm());
                        capPriArInfoDTO.setId(dto.getId());
                        capPriArInfoDTO.setMainTypCd(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_1);
                        list.add(capPriArInfoDTO);
                    }
                }
            }
        }

        return list;
    }
}
